package com.recommend.controller.admin;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.recommend.common.LoginInfo;
import com.recommend.domain.AdminDomain;
import com.recommend.dto.admin.ChangePwdDto;
import com.recommend.service.LoginService;

/**
 * Created by lw.
 */
@RestController
@RequestMapping("/admin")
public class LoginController {

    private final static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Resource
    private LoginService loginService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public String loginResult(AdminDomain adminDomain){
        logger.info("===============================admin: account={}, password={}==================", adminDomain.getAccount(), adminDomain.getPassword());
        AdminDomain unit = loginService.select(adminDomain);
        if(unit != null){
            return LoginInfo.LOGIN_SUCCESS;
        }else{
            return LoginInfo.LOGIN_FAILD;
        }
    }

    @RequestMapping(value = "/confirmPwd", method = RequestMethod.POST)
    @ResponseBody
    public String confirmPwd(ChangePwdDto changePwdDto){
        AdminDomain adminDomain = new AdminDomain();
        adminDomain.setAccount(changePwdDto.getAccount());
        adminDomain.setPassword(changePwdDto.getPassword());
        logger.info("===============================admin: account={}, password={}=======================", adminDomain.getAccount(), adminDomain.getPassword());
        AdminDomain unit = loginService.select(adminDomain);
        if(unit != null){
            return LoginInfo.LOGIN_SUCCESS;
        }else{
            return LoginInfo.LOGIN_FAILD;
        }
    }

    @RequestMapping(value = "/changePwd", method = RequestMethod.POST)
    @ResponseBody
    public int changePwd(ChangePwdDto changePwdDto){
        AdminDomain adminDomain = new AdminDomain();
        adminDomain.setAccount(changePwdDto.getAccount());
        adminDomain.setPassword(changePwdDto.getNewPassword());
        logger.info("===============================admin: account={}, password={}=======================", adminDomain.getAccount(), adminDomain.getPassword());
        return loginService.updateByAccount(adminDomain);
    }
}
